   package com.javalong.affairs.module.userModule.activity;

import java.util.ArrayList;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.model.Dept;
import com.javalong.affairs.module.userModule.fragment.UpdatePwdFragment;
import com.javalong.affairs.module.userModule.fragment.UpdateUserInfoFragment;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.ui.TopTitle.OnLeftDrawableClick;
import com.javalong.affairs.base.ui.TopTitle.OnRightTextClick;
import com.javalong.affairs.base.utils.AppUtils;
import com.javalong.affairs.base.utils.DisplayUtils;
import com.javalong.affairs.base.viewpagerfragment.MyFragmentPagerAdapter;

/**
 * 
 */
public class UserInfoActivity extends FragmentActivity {
	private static final String TAG = "MainActivity";
	private ViewPager mPager;
	private ArrayList<Fragment> fragmentsList;
	private ImageView ivBottomLine;
	private TextView tvTabActivity, tvTabGroups;

	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;
	private Resources resources;
	private TopTitle topTitle;
	public Dept dept;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_newuserinfo);
		resources = getResources();
		InitWidth();
		InitTextView();
		InitViewPager();
	}

	private void InitTextView() {
		tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
		tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
		topTitle = (TopTitle) findViewById(R.id.ui_toptitle);
		tvTabActivity.setOnClickListener(new MyOnClickListener(0));
		tvTabGroups.setOnClickListener(new MyOnClickListener(1));
		topTitle.setOnLeftDrawableClick(new OnLeftDrawableClick() {
			@Override
			public void leftClick() {
				finish();
			}
		});
		topTitle.setOnRightTextClick(new OnRightTextClick() {

			@Override
			public void rightTextClick() {
				AppUtils.getInstance().exit();
			}
		});

	}

	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
		fragmentsList = new ArrayList<Fragment>();
		fragmentsList.add(new UpdateUserInfoFragment());
		fragmentsList.add(new UpdatePwdFragment());
		LayoutInflater mInflater = getLayoutInflater();
		View activityView = mInflater.inflate(R.layout.lay1, null);
		mPager.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentsList));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	private void InitWidth() {
		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2);
		Log.i("MainActivity", "offset=" + offset);

		position_one = (int) (screenW / 4.0);
		position_two = position_one * 2;
		position_three = position_one * 3;
	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			float width = DisplayUtils.getOutMetrics().widthPixels
					- DisplayUtils.dip2px(UserInfoActivity.this, 40 * 4);
			float offsetLeft = DisplayUtils.dip2px(UserInfoActivity.this,
					40 * 2) + width / 2;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(offsetLeft, 0, 0, 0);
				}
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, offsetLeft, 0, 0);
				}
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}
}